import redis, time
from django.http import HttpResponse
from goods.models import SKU
from django.http import JsonResponse
pool = redis.ConnectionPool(host="localhost", port=6379, db=0)
r = redis.Redis(connection_pool=pool)
def test_cors(request):
    return HttpResponse("helloworld!")

def stock_view(request):
    """测试redis分布式锁"""
    # redis分布式锁【只有1把】
    with r.lock("dashopt:stock", blocking_timeout=5) as lock:
        sku = SKU.objects.get(id=1)
        sku.stock -= 1
        sku.sales += 1
        time.sleep(2)
        sku.save()
    return JsonResponse({"code":200})